<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2018/12/19
 * Time: 下午6:00
 */

namespace App\Http\Controllers;

use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Services\FriendsServices;
use Illuminate\Http\Request;
use App\Lib\Response;

class FriendsController extends BaseController
{
    private $services_friends;
    public function __construct()
    {
        $this->services_friends = new FriendsServices();
    }

    public function get_follows(Request $request){

        if(!$request->get('page_size')){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $user_id = $request->get("user_id");
        $page_index = $request->get("page_index");
        $page_size = $request->get("page_size");

        if(!$request->get('user_id')){
            $user_id = 0;
        }
        if(!$request->get('page_index')){
            $page_index = 0;
        }

        $data = $this->services_friends->get_follows($user_id, $page_index, $page_size);

        Response::returnResult(Hint::SUCCESS, $data);
    }

    public function get_fans(Request $request){
        if(!$request->get('page_size')){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $user_id = $request->get("user_id");
        $page_index = $request->get("page_index");
        $page_size = $request->get("page_size");

        if(!$request->get('user_id')){
            $user_id = 0;
        }
        if(!$request->get('page_index')){
            $page_index = 0;
        }

        $data = $this->services_friends->get_fans($user_id, $page_index, $page_size);

        Response::returnResult(Hint::SUCCESS, $data);
    }

    public function get_browses(Request $request){
        if(!$request->get('page_size')){
            Response::returnResult(Hint::PARAM_ERROR, []);
            return;
        }

        $user_id = $request->get("user_id");
        $page_index = $request->get("page_index");
        $page_size = $request->get("page_size");

        if(!$request->get('user_id')){
            $user_id = 0;
        }
        if(!$request->get('page_index')){
            $page_index = 0;
        }

        $data = $this->services_friends->get_browses($user_id, $page_index, $page_size);

        Response::returnResult(Hint::SUCCESS, $data);
    }

    public function get_visitors(Request $request){
        if(!$request->get('page_size')){
            Response::returnResult(Hint::PARAM_ERROR, []);
            return;
        }

        $user_id = $request->get("user_id");
        $page_index = $request->get("page_index");
        $page_size = $request->get("page_size");

        if(!$request->get('user_id')){
            $user_id = 0;
        }
        if(!$request->get('page_index')){
            $page_index = 0;
        }

        $data = $this->services_friends->get_visitors($user_id, $page_index, $page_size);

        Response::returnResult(Hint::SUCCESS, $data);
    }

    public function get_friends(Request $request){
        if(!$request->get('page_size')){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $user_id = $request->get("user_id");
        $page_index = $request->get("page_index");
        $page_size = $request->get("page_size");

        if(!$request->get('user_id')){
            $user_id = 0;
        }
        if(!$request->get('page_index')){
            $page_index = 0;
        }

        $data = $this->services_friends->get_friends($user_id, $page_index, $page_size);

        Response::returnResult(Hint::SUCCESS, $data);
    }

    public function post_follow(Request $request){
        if(!$request->get('user_id')){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $user_id = $request->get("user_id");
        $loginUserId = $this->getLoginUserId();//获取用户id
        if (empty($loginUserId)){
            Response::returnResult(Hint::LOGIN_OUT);
        }

        $cacheKey = 'USER:FOLLOW:' . $loginUserId.":".$user_id;
        if (!redis_lock($cacheKey, 1)) {
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $ret = null;
        if(!$request->get("action")){
            $ret = $this->services_friends->add_follow($loginUserId, $user_id);
        }else{
            $ret = $this->services_friends->del_follow($loginUserId, $user_id);
        }

        if (!$ret){
            Response::returnResult(Hint::LOGIN_OUT);
        }

        $data['is_friends']= isset($ret['is_friends']) ? $ret['is_friends'] : false;
        $data['private_msg'] = $ret['private_msg'] ?? '';
        Response::returnResult(Hint::SUCCESS, $data);
    }


    /**
     * 黑名单列表
     * @param Request $request
     */
    public function get_blocks(Request $request){
        if(!$request->get('page_size')){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $user_id = $request->get("user_id");
        $page_index = $request->get("page_index");
        $page_size = $request->get("page_size");

        if(!$request->get('user_id')){
            $user_id = 0;
        }
        if(!$request->get('page_index')){
            $page_index = 0;
        }

        $data = $this->services_friends->get_blocks($user_id, $page_index, $page_size);

        Response::returnResult(Hint::SUCCESS, $data);
    }

    /**
     * 黑名单添加删除
     * @param Request $request
     */
    public function post_block(Request $request){
        if(!$request->get('user_id')){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $user_id = $request->get("user_id");
        $ret = null;
        if(!$request->get("action")){
            $ret = $this->services_friends->add_block($user_id);
        }
        else{
            $ret = $this->services_friends->del_block($user_id);
        }
        if (!$ret){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 判断用户是否上黑名单
     */
    public function getUserInBlock(Request $request){
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $toUserId = $request->post('to_user_id');
        if(empty($toUserId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->services_friends->getUserInBlock($userId, $toUserId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 添加举报
     * @param Request $request
     */
    public function post_report(Request $request){
        $loginUserId = $this->getLoginUserId();
        if (empty($loginUserId)){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $user_id = $request->input("user_id");
        $report_type = $request->input("report_type");
        $pics = (string) $request->input("pics", '');
        $describe = (string) $request->input("describe", '');

        if (empty($user_id)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if (empty($report_type)){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //锁
        $lockKey = 'FRIENDS:userReport:'.$loginUserId.':'.$user_id;
        if (!redis_lock($lockKey, 1)){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, [], '');
        }

        $ret = $this->services_friends->add_report($loginUserId, $user_id, $report_type, $pics, $describe);
        if (!$ret){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 用户关系各种数量
     */
    public function get_counts(){
        $ret = $this->services_friends->get_counts();
        if ($ret == Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        Response::returnResult(Hint::SUCCESS, $ret);
    }

    ################# 后台调用 ##########################################

    /**
     * 用户举报更新
     * */
    public function reportUpdate()
    {
        do {
            if (!$this->checkRequest(false)) {
                break;
            }

            $this->callCommon($this->services_friends, 'reportUpdate', $this->requestData);

        } while (false);

        $this->responseData();
    }

    public function getFriendMsgAuth()
    {
        do {
            if (!$this->checkRequest(false)) {
                break;
            }

            $this->callCommon($this->services_friends, 'getFriendMsgAuth', $this->requestData);

        } while (false);

        if(is_string($this->responseData)) $this->responseData = json_decode($this->responseData, true);
        Response::returnResult($this->responseData['code'], $this->responseData['data'], $this->responseData['msg']);
    }

    /**
     * 获取用户之间的关系
     */
    public function getUserRelation(Request $request){
        $toUserId = $request->get('to_user_id');
        if(empty($toUserId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->services_friends->getUserRelation($toUserId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    ################# 后台调用 End ##########################################

    public function getNoticeNum()
    {
        do {
            if (!$this->checkRequest()) {
                break;
            }

            $this->callCommon($this->services_friends, 'getNoticeNum', $this->requestData);

        } while (false);

        if(is_string($this->responseData)) $this->responseData = json_decode($this->responseData, true);
        Response::returnResult($this->responseData['code'], $this->responseData['data'], $this->responseData['msg']);
    }


    /**
     * 用户发起私聊前校验并获得相互的关系
     */
    public function checkUserSendSms(Request $request)
    {
        $userId = $this->getLoginUserId();
        if (empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $toUserId = (int) $request->get("user_id");//接收消息用户id
        if (empty($toUserId)) {
            Response::returnResult(Hint::PARAM_ERROR, [], '用户参数错误');
        }

        $data = $this->services_friends->checkUserSendSms($userId, $toUserId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }
        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 获取用户亲密关系列表
     */
    public function getUserRelationList(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $toUserId = (int) $request->input("to_user_id");
        if(empty($toUserId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->services_friends->getUserRelationList($userId, $toUserId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    public function getFriendList(Request $request) {
        $pageIndex = $request->get("pageIndex") ?? 0;
        $pageSize = $request->get("pageSize") ?? 10;

        $userId = $this->getLoginUserId();
        $return = $this->services_friends->getFriendList($userId, $pageIndex, $pageSize);
        Response::returnResult(Hint::SUCCESS, $return);
    }

}